package com.changgou.service.goods.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.common.pojo.PageResult;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.mapper.BrandMapper;
import com.changgou.service.goods.service.BrandService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: Bytezero_zhengLei
 * @Date: 2024/8/25 17:15
 * @Project_Name: BrandServiceImpl.java
 * @Email: 420498246@qq.com
 * @Description:
 * @TODO: 接口实现
 */

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Resource
    private BrandMapper brandMapper;

    @Override
    public List<Brand> search(Map<String, Object> searchMap) {
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();
        if (searchMap.containsKey("name")) {
            queryWrapper.like(searchMap.get("name") != null, Brand::getName, searchMap.get("name"));
        }
        if (searchMap.containsKey("letter")) {
            queryWrapper.eq(searchMap.get("letter") != null, Brand::getLetter, searchMap.get("letter"));
        }
        return brandMapper.selectList(queryWrapper);
    }


    @Override
    public PageResult<Brand> findPage(Integer page, Integer size) {
        Page<Brand> p = new Page<>(page, size);
        Page<Brand> brandPage = (Page<Brand>) brandMapper.selectPage(p, new LambdaQueryWrapper<>());
        PageResult<Brand> pageResult = new PageResult<>();
        pageResult.setTotal(brandPage.getTotal());
        pageResult.setRows(brandPage.getRecords());
        return pageResult;

    }

    @Override
    public PageResult<Brand> queryPage(Map<String, Object> searchMap, int page, int size) {
        Page<Brand> p = new Page<>(page, size);
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();
        if (searchMap.containsKey("name")) {
            queryWrapper.like(searchMap.get("name") != null && searchMap.get("name") != "", Brand::getName, searchMap.get("name"));
        }
        if (searchMap.containsKey("letter")) {
            queryWrapper.eq(searchMap.get("letter") != null && searchMap.get("letter") != "", Brand::getLetter, searchMap.get("letter"));
        }
        Page<Brand> brandPage = (Page<Brand>) brandMapper.selectPage(p, queryWrapper);
        PageResult<Brand> pageResult = new PageResult<>();
        pageResult.setTotal(brandPage.getTotal());
        pageResult.setRows(brandPage.getRecords());
        return pageResult;
    }

    @Override
    public List<Map> findListByCategoryName(String categoryName) {
        return brandMapper.findListByCategoryName(categoryName);
    }

}
